﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DatabaseHelper;

namespace UI.Online.SubForms
{
    public partial class frmNewPipeline : Form
    {
        public PProject CurrentProject;
        public string PipelineName;
        public int ProjectId;

        public frmNewPipeline()
        {
            InitializeComponent();
        }

        public frmNewPipeline(int projectId, PProject project)
            : this()
        {
            ProjectId = projectId;
            CurrentProject = project;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddNewPipeline(string PipelineName, string PipelineSummary)
        {
            if (PipelineName != "")
            {
                DBHelper dh = new DBHelper();
                PPipeline pip = new PPipeline();
                pip.PipelineName = PipelineName;
                pip.PipelineSummary = PipelineSummary;

                dh.Query = "SELECT * FROM Pipelines WHERE PipelineName = '" + PipelineName + "' AND ProjectID = " + ProjectId;
                DataTable dt = new DataTable();
                dt = dh.Fill();
                if (dt.Rows.Count == 0)
                {
                    dh.Add(pip, ProjectId);
                }
                else
                {
                    MessageBox.Show("The pipeline '" + PipelineName + "' already exist.\n Please create new pipeline with different name.",
                        "Duplucated Pipelines",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation,
                        MessageBoxDefaultButton.Button1);
                }
            }
            else
            {
                MessageBox.Show("Project name and location must not be empty.");
            }
        }

        private void frmNewPipeline_Load(object sender, EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            AddNewPipeline(txtPipelineName.Text, rtfPipelineSummary.Text);
            string pipelineFolder = CurrentProject.Location + @"\" + PipelineName;
            if (!System.IO.Directory.Exists(pipelineFolder))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(pipelineFolder);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            this.Close();
        }
    }
}
